local skel = fk.CreateSkill {
  name = "rmt__chengxia",
}

skel:addEffect("viewas", {
  anim_type = "offensive",
  pattern = "slash,fire_attack,lightning",  --仅留三个牌名以供回合外触发使用
  prompt = function (self, player, selected_cards, selected)
    return "#rmt__chengxia:::"..player:getMark(skel.name)
  end,
  card_filter = function (self, player, to_select, selected)
    return #selected == 0
  end,
  view_as = function (self, player, cards)
    if #cards ~= 1 then return end
    local card = Fk:cloneCard(Self:getMark(skel.name))
    card.skillName = skel.name
    card:addSubcard(cards[1])
    return card
  end,
  enabled_at_play = function(self, player)
    local current = Fk:currentRoom().current
    if player:getMark("rmt__chengxia_invalid-phase") ~= 0 or not (current and current.phase == Player.Play) then return false end
    return player:getMark(skel.name) ~= 0
  end,
  enabled_at_response = function (self, player, response)
    local current = Fk:currentRoom().current
    if player:getMark("rmt__chengxia_invalid-phase") ~= 0 or not (current and current.phase == Player.Play) then return false end
    if not response and player:getMark(skel.name) ~= 0 then
      if Fk.currentResponsePattern then
        return Exppattern:Parse(Fk.currentResponsePattern):matchExp(player:getMark(skel.name))
      else
        return player:canUse(Fk:cloneCard(player:getMark(skel.name)))
      end
    end
  end,
})

skel:addAcquireEffect(function (self, player, is_start)
  local room = player.room
  local name = {}
  room.logic:getActualDamageEvents(1, function(e)
    local damage = e.data
    if damage.to == player and damage.card then
      table.insert(name, damage.card.name)
      return true
    end
  end, Player.HistoryGame, 1)
  room:setPlayerMark(player, "rmt__chengxia", #name > 0 and name[1] or 0)
end)


skel:addEffect(fk.AfterCardsMove, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(skel.name) then
      local cards = {}
      for _, move in ipairs(data) do
        if move.from == player then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerJudge and
              info.extra_data and info.extra_data.rmt__chengxia == player.id and
              not table.contains(player:getCardIds("h"), info.cardId) then
              table.insertIfNeed(cards, info.cardId)
            end
          end
        end
      end
      if #cards > 0 then
        event:setCostData(self, {cards = cards})
        return true
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:delay(600)
    room:moveCardTo(event:getCostData(self).cards, Card.PlayerHand, player, fk.ReasonPrey, skel.name, nil, true, player)
  end,
})

--FIXME: 伪实现以获得转化闪电
skel:addEffect(fk.PreCardEffect, {
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(skel.name) then
      if data.to == player and data.card.name == "lightning" and data.card:isVirtual() and #data.card.subcards > 0 then
        event:setCostData(self, {cards = data.card.subcards})
        return true
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:delay(600)
    room:moveCardTo(event:getCostData(self).cards, Card.PlayerHand, player, fk.ReasonPrey, skel.name, nil, true, player)
  end,
})

-- 额外指定一个目标
skel:addEffect(fk.AfterCardTargetDeclared, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(skel.name) then
      return target == player and table.contains(data.card.skillNames, "rmt__chengxia") and
        #data:getExtraTargets() > 0 and
        table.find(player.room.alive_players, function (p) return p:hasDelayedTrick("lightning") end)
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local to = room:askToChoosePlayers(player, {
      targets = data:getExtraTargets(),
      max_num = 1, min_num = 1, skill_name = skel.name, cancelable = true,
      prompt = "#rmt__chengxia-choose:::"..data.card:toLogString(),
    })
    if #to > 0 then
      event:setCostData(self, {tos = to})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    data:addTarget(event:getCostData(self).tos[1])
  end,
})

-- 记录受到伤害的牌
skel:addEffect(fk.Damaged, {
  can_refresh = function(self, event, target, player, data)
    return target == player and player:hasSkill(skel.name, true) and data.card
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:setPlayerMark(player, "rmt__chengxia", data.card.name)
  end,
})

skel:addEffect(fk.BeforeCardsMove, {
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(skel.name, true) and player:hasDelayedTrick("lightning")
  end,
  on_refresh = function(self, event, target, player, data)
    for _, move in ipairs(data) do
      if move.from == player then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerJudge and
            (Fk:getCardById(info.cardId).trueName == "lightning" or
            (player:getVirtualEquip(info.cardId) and player:getVirtualEquip(info.cardId).trueName == "lightning")) then
              --转化闪电在判定阶段移动到处理区的移动事件中card信息完全丢失
            info.extra_data = info.extra_data or {}
            info.extra_data.rmt__chengxia = player.id
          end
        end
      end
    end
  end,
})

-- 判断本阶段是否使用过牌
skel:addEffect(fk.CardUsing, {
  can_refresh = function(self, event, target, player, data)
    if target == player and player:hasSkill(skel.name, true) and player:getMark("rmt__chengxia_invalid-phase") == 0 then
      local current = player.room.current
      return current and current.phase == Player.Play
    end
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:setPlayerMark(player, "rmt__chengxia_invalid-phase", 1)
  end,
})


Fk:loadTranslationTable{
  ["rmt__chengxia"] = "城狭",
  [":rmt__chengxia"] = "一名角色的出牌阶段，若你此阶段未使用过牌，你可以将一张牌当上次对你造成伤害的牌使用，若场上有【闪电】，此牌可以额外"..
  "指定一个目标。当【闪电】离开你的判定区后，你获得之。",

  ["#rmt__chengxia"] = "城狭：你可以将一张牌当【%arg】使用",
  ["#rmt__chengxia-choose"] = "城狭：是否为%arg额外指定一个目标？",

  ["$rmt__chengxia1"] = "六国复叛，朕下敕令，不出阿房一宫...",
  ["$rmt__chengxia2"] = "四面秦歌，朕所履踏，唯余咸阳一城...",
}

return skel
